package com.eastdigit.system.service;

import com.eastdigit.data.DataSourceId;
import com.eastdigit.data.DataTable;
import com.eastdigit.data.PagedDataTable;
import com.eastdigit.data.QueryBuilder;
import com.eastdigit.system.model.SqlBuilder;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class BaseService {

    @Autowired
    public DataSourceId dataSourceId;
    @Autowired
    protected JdbcTemplate jdbcTemplate;
    @Autowired
    public DataSourceId dataSource;

    protected Integer intId() {
        return dataSourceId.getId();
    }

    protected String stringId() {
        return String.valueOf(intId());
    }

    /**
     * 表自增主键
     * 
     * @param tableName
     * @param idName
     * @return
     */
    public synchronized String intIncrementId(String tableName, String idName) {
        int maxInt = 0;
        String dbType = dataSource.getDataBase();
        if (null != dbType && dbType.contains("oracle")) {// oracle
            maxInt = executeInt("select max(to_number(" + idName + ")) from " + tableName);
        } else if (null != dbType && dbType.contains("mysql")) {// MySQL
            maxInt = executeInt("select max(CONVERT(" + idName + ", SIGNED)) from " + tableName);
        }

        String maxStr = (maxInt + 1) + "";
        return maxStr;
    }

    protected String executeString(String sql, Object... params) {
        return new QueryBuilder(jdbcTemplate, sql, params).executeString();
    }

    protected int executeInt(String sql, Object... params) {
        return new QueryBuilder(jdbcTemplate, sql, params).executeInt();
    }

    protected JSONArray query(String sql) {
        return query(sql, null);
    }

    protected JSONArray query(SqlBuilder sqlBuilder) {
        return query(sqlBuilder.getSql(), sqlBuilder.getParams());
    }

    protected JSONArray query(String sql, Object... params) {
        return new QueryBuilder(jdbcTemplate, sql, params).executeDataTable().toJSONArray();
    }

    protected void delete(String sql, Object... params) {
        jdbcTemplate.update(sql, params);
    }

    protected void delete(SqlBuilder sqlBuilder) {
        jdbcTemplate.update(sqlBuilder.getSql(), sqlBuilder.getParams());
    }

    protected void update(String sql, Object... params) {
        jdbcTemplate.update(sql, params);
    }

    /**
     * queryTable
     * 
     * @param sql
     * @param params
     * @param pageSize
     * @param pageIndex
     * @return
     */
    protected JSONObject queryTable(String sql, Object[] params, int pageSize, int pageIndex) {
        JSONObject json = new JSONObject();
        if (pageSize == 0) {// 不分页
            DataTable table = new QueryBuilder(jdbcTemplate, sql, params).executeDataTable();
            json.put("nextFlag", "N");
            json.put("total", table.getRowCount());// 总条数
            json.put("rows", table.toJSONArray());
        } else {
            QueryBuilder qb = new QueryBuilder(jdbcTemplate, sql, params);
            PagedDataTable paged = qb.executePagedDataTable(pageSize, pageIndex);
            json.put("nextFlag", paged.getTotalRows() > (pageIndex + 1) * pageSize ? "Y" : "N");
            json.put("total", paged.getTotalRows());// 总条数
            json.put("rows", paged.getDataTable().toJSONArray());
        }
        return json;
    }

    protected JSONObject queryTable(SqlBuilder sqlBuilder, int pageSize, int pageIndex) {
        return this.queryTable(sqlBuilder.getSql(), sqlBuilder.getParams(), pageSize, pageIndex);
    }

}
